import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { Storage } from '@ionic/Storage';


const subject: Subject<any> = new Subject<any>();


@Injectable({
  providedIn: 'root',
})
export class EmitService {
  _deviceLift;         // 电梯（设备）
  _roomGroup;         // 供配电、给排水（房间）
  _deviceParams;      // 供配电、给排水（设备）
  _installParams;     // 一键部署安装信息
  _installState;      // 一键部署状态刷新
  _installStack;      // 选择的父级仪表
  _setGroups;         // 集团界面选择的项目


  constructor(public storage: Storage,
  ) {
  }

  // 参数
  // 房间 --> 设备(电梯)
  set deviceLift(val) {
    this.setLocalstorageValue('deviceLift', val);
  }

  get deviceLift() {
    return this.getLocalstorageValue('deviceLift');
  }

  // 系统 --> 房间(供配电、给排水)
  set roomGroup(val) {
    this.setLocalstorageValue('roomGroup', val);
  }

  get roomGroup() {
    return this.getLocalstorageValue('roomGroup');
  }

  // 房间 --> 设备(供配电、给排水)
  set deviceParams(val) {
    this.setLocalstorageValue('deviceParams', val);
  }

  get deviceParams() {
    return this.getLocalstorageValue('deviceParams');
  }

  set installParams(val) {
    this.setLocalstorageValue('installParams', val);
  }

  get installParams() {
    return this.getLocalstorageValue('installParams');
  }

  set installState(val) {
    this.setLocalstorageValue('installState', val);
  }

  get installState() {
    return this.getLocalstorageValue('installState');
  }

  set installStack(val) {
    this.setLocalstorageValue('installStack', val);
  }

  get installStack() {
    return this.getLocalstorageValue('installStack');
  }

  set setGroups(val) {
    this.setLocalstorageValue('setGroups', val);
  }

  get setGroups() {
    return this.getLocalstorageValue('setGroups');
  }


  // 获取存储在localstorage中的数据,同步返回
  getLocalstorageValue(name) {
    const storeName = '_' + name;
    if (this[storeName]) {
      if (this.isJsonString(this[storeName])) {
        return JSON.parse(this[storeName]);
      }
      return this[storeName];
    } else {
      const val = localStorage.getItem(storeName);
      if (val) {
        if (this.isJsonString(val)) {
          this[storeName] = JSON.parse(val);
        } else {
          this[storeName] = val;
        }
        return this[storeName];
      } else {
        return null;
      }
    }
  }

  // 设置存储在localstorage中的数据
  setLocalstorageValue(name, value) {
    const storeName = '_' + name;
    if (typeof value === 'string') {
      if (value === '') {
        this[storeName] = null;
        localStorage.removeItem(storeName);
      } else {
        this[storeName] = value;
        localStorage.setItem(storeName, value);
      }
    } else if (typeof value === 'object') {
      this[storeName] = value;
      localStorage.setItem(storeName, JSON.stringify(value));
    }
    subject.next(value);
  }

  isJsonString(str) {
    try {
      if (typeof JSON.parse(str) === 'object') {
        return true;
      }
    } catch (e) {
    }
    return false;
  }

  clearCache() {
    for (const item in localStorage) {
      if (item) {
        console.log(item)
        // console.log(localStorage[item]);
        localStorage.removeItem(item);
      }
    }
    this.storage.clear();
  }
}
